using System;
using System.Collections.Generic;
using System.Threading;

namespace PaulRz.Threading
{
    /// <summary>
    /// This queue is synchronized and doesn't allow null elements to be enqueued
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public sealed class SynchronizedQueue<T> : Queue<T>, IDisposable
        where T : class
    {
        private AutoResetEvent waitEvent = new AutoResetEvent(false);

        /// <summary>
        /// Create default synchronized queue
        /// </summary>
        public SynchronizedQueue()
        {
        }

        /// <summary>
        /// Create synchronized queue with capacity defined
        /// </summary>
        /// <param name="capacity"></param>
        public SynchronizedQueue(int capacity) : base(capacity)
        {
        }

        /// <summary>
        /// Create synchronized queue from collection
        /// </summary>
        /// <param name="collection"></param>
        public SynchronizedQueue(IEnumerable<T> collection) : base(collection)
        {
        }        
        
        /// <summary>
        /// Stops the queue work
        /// </summary>
        public void Stop()
        {
            if (waitEvent!=null)
            {
                waitEvent.Close();
                waitEvent = null;
            }
            
        }

        /// <summary>
        /// Starts the queue work
        /// </summary>
        public void Start()
        {
            if (waitEvent==null)
                waitEvent = new AutoResetEvent(false);
        }
        
        /// <summary>
        /// Synchronization object
        /// </summary>
        public object SyncRoot
        {
            get
            {
                return this;
            }
        }
        
        /// <summary>
        /// Enqueue the object
        /// </summary>
        /// <param name="item"></param>
        public new void Enqueue(T item)
        {
            if (item != null)
            {
                lock (SyncRoot)
                {
                    base.Enqueue(item);

                    waitEvent.Set();
                }
            }
        }

        /// <summary>
        /// The amount of items in the queue
        /// </summary>
        public new int Count
        {
            get
            {
                lock(SyncRoot)
                    return base.Count;
            }
        }
        
        /// <summary>
        /// Clears the queue
        /// </summary>
        public new void Clear()
        {
            lock(SyncRoot)
                base.Clear();
        }
        
        /// <summary>
        /// Dequeue the item from the queue
        /// </summary>
        /// <returns></returns>
        public new T Dequeue()
        {
            T item = default(T);

            while (item == null)
            {
                while (base.Count == 0)
                    waitEvent.WaitOne();

                lock (SyncRoot)
                {
                    if (base.Count > 0)
                        item = base.Dequeue();
                }

                if (base.Count > 0)
                    waitEvent.Set();
            }

            return item;
        }

        ///<summary>
        ///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        ///</summary>
        ///<filterpriority>2</filterpriority>
        public void Dispose()
        {
            if (waitEvent!=null)
            {
                lock(this)
                {
                    waitEvent.Close();
                    waitEvent = null;
                }
                GC.SuppressFinalize(this);
            }
        }
    }
}
